#include <docker.h>
/**
 * 登录阿里云的 Docker 服务
 *
 * @param username 用户名
 * @param password 密码
 * @param cmd 保存命令
 * @return 登录结果，0 表示成功，-1 表示失败
 */
int login_aliyuncs_docker(const char *username,const char *password,string& cmd){
    // 登录阿里云 docker 服务
    cmd+="\n";
    cmd += "echo "+string(password)+" | docker login --username="+string(username)+" --password-stdin ";
    cmd +="crpi-vb403qyxdijc0eq9.cn-guangzhou.personal.cr.aliyuncs.com \n";
    return 0;
}
/**
 * 配置阿里云源
 *
 * @param cmd 保存命令
 * @return 配置结果，0 表示成功，-1 表示失败
 */
int sources_list(string& cmd){
    // 配置阿里云源
    cmd+="\n";
    cmd+="RUN echo \"deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse\" > /etc/apt/sources.list && \\\n";
    cmd+="echo \"deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse\" >> /etc/apt/sources.list && \\\n";

    cmd+="echo \"deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse\" >> /etc/apt/sources.list && \\\n";
    cmd+="echo \"deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse\" >> /etc/apt/sources.list && \\\n";

    cmd+="echo \"deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse\" >> /etc/apt/sources.list && \\\n";
    cmd+="echo \"deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse\" >> /etc/apt/sources.list && \\\n";

    cmd+="echo \"deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse\" >> /etc/apt/sources.list && \\\n";
    cmd+="echo \"deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse\" >> /etc/apt/sources.list && \\\n";
    cmd+="apt-get update -y  && \\\n";
    return 0;

}

/**
 * 构建镜像
 *
 * @param buildDir 构建目录
 * @param dockerfile Dockerfile 内容
 * @param local_repository 本地仓库
 * @param local_tag 本地标签
 * @param save_repository 保存仓库
 * @param save_tag 保存标签
 * @return 构建结果，0 表示成功，-1 表示失败
 */
int build_image(string buildDir,string dockerfile,string local_repository,string local_tag,string save_repository,string save_tag){
    string fileName=buildDir+"/Dockerfile";
   // string fileName="/opt/work/make/util-docker/main/doc/dockerFile/ssh_ubuntu_22_04/Dockerfile";
    stringWriteFile(fileName.c_str(),dockerfile);
    string cmd="#!/bin/bash \n";
    if(login_aliyuncs_docker("qq1813033378","qq..1813033378",cmd) !=0)return -1;
    // 删除 阿里 镜像
    delete_image(save_repository,save_tag,cmd);
    // 删除 本地 镜像
    delete_image(local_repository,local_tag,cmd);
    // 构建镜像 命令
    cmd += "docker build -f "+fileName+" --progress=plain -t ";
    cmd +=local_repository+":"+local_tag+" "+buildDir+" \n ";

    cmd += "if [ $? -ne 0 ]; then \n";
    cmd += "echo \"构建镜像失败\" \n";
    cmd += "exit -1 \n";
    cmd += "fi \n";

    // docker 通过镜像 REPOSITORY TAG 获取id
    cmd += "IMAGEID=$(docker images --format ";
    cmd += "\"{{.ID}}\t{{.Repository}}:{{.Tag}}\" | grep \""+local_repository+":"+local_tag;
    cmd += "\" | awk '{print $1}') \n";
    // 镜像保存命令
    cmd += "docker tag $IMAGEID "+save_repository+":"+save_tag+" \n";
    // 镜像推送命令
    cmd += "docker push "+save_repository+":"+save_tag+" \n";
    return system(cmd.c_str());
   //return 0;
}

/**
 * @brief 删除镜像
 * @param repository 镜像名称
 * @param tag 镜像标签
 * @param cmd 保存命令
 * @return int
 */
int delete_image(string repository,string tag,string& cmd ){
    // 删除镜像
    cmd +="\n";
    cmd +="if docker images \""+repository+":"+tag+"\" | grep -q \""+repository+"\"; then\n";
    cmd += "DEL_ID=$(docker images --format ";
    cmd += "\"{{.ID}}\t{{.Repository}}:{{.Tag}}\" | grep \""+repository+":"+tag;
    cmd += "\" | awk '{print $1}') \n";
    cmd+="docker rmi -f \"$DEL_ID\" \n";
    cmd+="fi \n";
    return 0;
}



